<?

/***************************************************************************
 Jupiter Content System @ Jupiterportal.com
 Copyright (C) 2005 Cosmin Flavius (highstrike@gmail.com)

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 More Info About The Licence At http://www.gnu.org/copyleft/gpl.html
****************************************************************************/

function b_calendar()
{
	$name_function = "b_calendar";

	global $a, $db, $blocks_db, $template, $language, $config, $misc, $PHP_SELF,
           $is_loged_in, $is_moderator, $is_administrator, $is_webmaster, $months,
           $days, $m, $y, $OnlineGuestsCount, $OnlineMembersCount, $sitelanguage;
	if(!file_exists("blocks/$name_function/language/$sitelanguage.php"))
        include "blocks/$name_function/language/en.php";
	else include "blocks/$name_function/language/$sitelanguage.php";

	$random_code = md5("$name_function");
	$random_code = substr("$random_code", 0, 5);

	if(!isset($content)) $content = NULL;
	if(!isset($_GET["$random_code"])) $_GET["$random_code"] = NULL;

	if($_GET["$random_code"] == "on") $_SESSION["$random_code"] = "on";
	elseif($_GET["$random_code"] == "off") $_SESSION["$random_code"] = "off";

	$query_string = "?{$_SERVER['QUERY_STRING']}";

	foreach($blocks_db as $blocks)
	{
		$block_code = md5("{$blocks['name']}");
		$block_code = substr("$block_code", 0, 5);

		$query_string = str_replace("&$block_code=off","", $query_string);
		$query_string = str_replace("&$block_code=on","", $query_string);
	}

	if($query_string == "?" || strpos($query_string, '=on') !== false || strpos($query_string, '=off') !== false) $code_query = "?";
	else $code_query = "$query_string&";

	if(!isset($_SESSION["$random_code"]) || $_SESSION["$random_code"] == "on") $links= "<a href='$code_query$random_code=off'><img src='templates/$template/images/misc/minus.gif' border='0'> {$language_block['Calendar title']}</a>";
	elseif($_SESSION["$random_code"] == "off") $links = "<a href='$code_query$random_code=on'><img src='templates/$template/images/misc/plus.gif' border='0'> {$language_block['Calendar title']}</a>";

	$template_block = implode("", file("templates/$template/block.php"));
	$template_block = str_replace("{template}", $template, $template_block);
	$template_block = str_replace("{links}", $links, $template_block);

	if(!isset($_SESSION["$random_code"]) || $_SESSION["$random_code"] == "on")
	{
		//-----------------------------------------------------------------
		//  Some Code parts
		//-----------------------------------------------------------------
        
        $days = array($language_block['Calendar day'], $language_block['Calendar day2'],
                      $language_block['Calendar day3'], $language_block['Calendar day4'],
                      $language_block['Calendar day5'], $language_block['Calendar day6'],
                      $language_block['Calendar day7']);
        $months = array($language_block['Calendar month'], $language_block['Calendar month'],
                        $language_block['Calendar month2'],$language_block['Calendar month3'],
                        $language_block['Calendar month4'],$language_block['Calendar month5'],
                        $language_block['Calendar month6'],$language_block['Calendar month7'],
                        $language_block['Calendar month8'],$language_block['Calendar month9'],
                        $language_block['Calendar month10'],$language_block['Calendar month11'],
                        $language_block['Calendar month12']);
                        
        $month = $m;
        if (!isset($y)) $year = date('Y');
        else $year = $y;
        
        $monthfulltext = date("F", mktime(0, 0, 0, $month, 1, $year));
        $day_in_mth = date("t", mktime(0, 0, 0, $month, 1, $year));
        
        $next_month = ($month % 12) + 1; 
        $prev_month = (($month + 10) % 12) +1;
        
        $next_year = $year;
        $prev_year = $year;
        
        if ($month == 12) $next_year += 1;
        if ($month == 1) $prev_year -= 1; 
        
        $result = $db -> query("SELECT id, type, day, month, year FROM events ".
                               "WHERE month = '$month' AND year = '$year' ".
                               "ORDER BY 'type'");
                               
        while ($temp = mysql_fetch_array($result))
		{
			$events[$temp['day']][] = $temp['id'];
			$event_info[$temp['id']] = $temp['type'];
			$event_year_db[$temp['id']] = $temp['year'];
		}

		//-----------------------------------------------------------------
		//  Start Display
		//-----------------------------------------------------------------

		$content .= "<tr class='bottom'><td colspan='7' align='center'>".
                    "<table width='100%' border='0' cellspacing='0' ".
                    "cellpadding='0'><col width='5%'><col width='90%'>".
                    "<col width='5%'><tr class='bottom'>".
                    "<td><a href='index.php?m=$prev_month&y=$prev_year'>&laquo;</a></td>".
                    "<td align='center'>{$months[$month]} $year</td>".
                    "<td><a href='index.php?m=$next_month&y=$next_year'>&raquo;</a></td>".
                    "</tr></table></td></tr><tr>";

		foreach($days as $day) $content .= "<td class='con3' align='center'>".substr("$day", 0, $language_block['Calendar daycut'])."</td>";

		$content .= "</tr><tr>";

		//-----------------------------------------------------------------
		//  For first empty spaces
		//-----------------------------------------------------------------

		$day_of_wk = date("w", mktime(0, 0, 0, $month, 1, $year));
		
		if (!$day_of_wk) $day_of_wk = 7;

		for ($i = 1; $i < $day_of_wk; $i++)
            $content .= "<td class='con1'>&nbsp;</td>";

		//-----------------------------------------------------------------
		//  Show days + events + birthdays
		//-----------------------------------------------------------------

		for ($date_of_mth = 1; $date_of_mth <= $day_in_mth; $date_of_mth++)
		{
			//-----------------------------------------------------------------
			//  Configure stuff first
			//-----------------------------------------------------------------

			$date_no = date("j", mktime(0, 0, 0, $month, $date_of_mth, $year));
			$day_of_wk = date("w", mktime(0, 0, 0, $month, $date_of_mth, $year));

			//-----------------------------------------------------------------
			//  Calculate class
			//-----------------------------------------------------------------

			if($month == date('n') && $date_no == date('j'))
                $class = "eventtoday";
			else if ($day_of_wk == 0 || $day_of_wk == 6) $class = "con5";
            else $class = "eventempty";
            
			//-----------------------------------------------------------------
			//  Show days
			//-----------------------------------------------------------------

			$day_event = "<td class='$class'>$date_no</td>";

			if(isset($events[$date_no]))
			{
				foreach ($events[$date_no] as $key=>$value)
				{
					if($date_no == date('j') && $month == date('n')) $day_event = "<td class='eventtoday'><a href='$PHP_SELF?n=modules/events&d=$date_no&m=$month'>$date_no</a></td>";
					else
					{
						if($event_info[$value] == 'normal') $day_event = "<td class='eventselect'><a href='$PHP_SELF?n=modules/events&d=$date_no&m=$month'>$date_no</a></td>";
						else $day_event = "<td class='eventbday'><a href='$PHP_SELF?n=modules/events&d=$date_no&m=$month'>$date_no</a></td>";
					}
				}
			}

			$content .= $day_event;

			//-----------------------------------------------------------------
			//  For last empty spaces
			//-----------------------------------------------------------------

			if($day_of_wk == 0) $content .= "</tr>";

			if($day_of_wk > 0 && $date_of_mth == $day_in_mth)
			{
				for ($i = $day_of_wk; $i < 7; $i++)
                    $content .= "<td class='con1'>&nbsp;</td>";
                
                $content .= "</tr>";
			}
		}
	}

	$template_block = str_replace("{content}", $content, $template_block);
	$output = $template_block;

return $output;
}

?>